﻿using Microsoft.Extensions.Configuration;
using Coldairarrow.Business.Base_Company;
using Coldairarrow.Entity.Base_Company;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.Base_Company
{
    [Route("/Base_Company/[controller]/[action]")]
    public class Base_ComapnyController : BaseApiController
    {
        #region DI
        readonly IConfiguration _configuration;
        public Base_ComapnyController(IBase_ComapnyBusiness base_ComapnyBus, IBase_ComapnyDataBusiness ComapnyDataBusiness, IConfiguration configuration)
        {
            _base_ComapnyBus = base_ComapnyBus;
            _configuration = configuration;
            _ComapnyDataBusiness = ComapnyDataBusiness;
        }

        IBase_ComapnyBusiness _base_ComapnyBus { get; }
        IBase_ComapnyDataBusiness _ComapnyDataBusiness { get; }

        #endregion

        #region 获取



        /// <summary>
        /// 获取子站名称列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public List<object> GetCompanyList()
        {
            return _base_ComapnyBus.GetCompanyList();
        }



        ///// <summary>
        ///// 获取列表
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public async Task<PageResult<Base_Comapny>> GetDataList(PageInput<ConditionDTO> input)
        //{
        //    return await _base_ComapnyBus.GetDataListAsync(input);
        //}

        /// <summary>
        /// 获取单个信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public CompanyUnitInfo GetTheModel(IdInputDTO input)
        {
            return _base_ComapnyBus.GetTheModel(input.id);
        }


        /// <summary>
        /// 获取站点基本信息
        /// </summary>
        /// <param></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Base_Comapny> GetTheData()
        {
            string id = "";
            return await _base_ComapnyBus.GetTheDataAsync(id);
        }


        /// <summary>
        /// 获取域名信息列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<Base_ComapnyData>> GetDetailsList(PageInput<ComapnyDataInputDTO> input)
        {
            return await _ComapnyDataBusiness.GetDataListAsync(input);
        }


        /// <summary>
        /// 获取域名信息单个详情
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Base_ComapnyData> GetDetailModel(IdInputDTO input)
        {
            return await _ComapnyDataBusiness.GetTheDataAsync(input.id);
        }
        #endregion


        #region 提交
        /// <summary>
        /// 保存/修改子站信息
        /// </summary>
        /// <param name="data">子站信息</param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveTheData(CompanyUnitInfo data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                Base_Comapny model = new Base_Comapny();
                model.Name = data.Name;
                model.Code = data.Code;
                model.LogoImgPath = data.LogoImgPath;
                model.ResUser = data.ResUser;
                model.ResPhone = data.ResPhone;
                model.IndexTemplateId = data.IndexTemplateId;
                model.ListTemplateId = data.ListTemplateId;
                model.ShowListTemplateId = data.ShowListTemplateId;
                model.HaveMember = data.HaveMember;
                model.HaveWaterMark = data.HaveWaterMark;
                model.LoginType = data.LoginType;
                model.SmsAccount = data.SmsAccount;
                model.SmsPassword = data.SmsPassword;
                model.SmsWebSite = data.SmsWebSite;
                model.KeepOne = data.KeepOne;
                model.KeepTwo = data.KeepTwo;
                model.KeepThree = data.KeepThree;
                model.KeepFour = data.KeepFour;

                InitEntity(model);

                await _base_ComapnyBus.AddDataAsync(model);
            }
            else
            {
                Base_Comapny model = _base_ComapnyBus.GetTheData(data.Id);

                model.Name = data.Name;
                model.Code = data.Code;
                model.LogoImgPath = data.LogoImgPath;
                model.ResUser = data.ResUser;
                model.ResPhone = data.ResPhone;
                model.IndexTemplateId = data.IndexTemplateId;
                model.ListTemplateId = data.ListTemplateId;
                model.ShowListTemplateId = data.ShowListTemplateId;
                model.HaveMember = data.HaveMember;
                model.HaveWaterMark = data.HaveWaterMark;
                model.LoginType = data.LoginType;
                model.SmsAccount = data.SmsAccount;
                model.SmsPassword = data.SmsPassword;
                model.SmsWebSite = data.SmsWebSite;
                model.KeepOne = data.KeepOne;
                model.KeepTwo = data.KeepTwo;
                model.KeepThree = data.KeepThree;
                model.KeepFour = data.KeepFour;

                await _base_ComapnyBus.UpdateDataAsync(model);
            }
        }


        /// <summary>
        /// 保存/修改子站基本信息
        /// </summary>
        /// <param name="data">子站信息</param>
        /// <returns></returns>
        [HttpPost]
        public async Task SetCompanyData(ComapnyInfo data)
        {
            if (string.IsNullOrEmpty(data.Id))
            {
                throw new BusException("子站Id参数不能为空！");
            }

            Base_Comapny model = _base_ComapnyBus.GetTheData(data.Id);
            model.Name = data.Name;  
            model.EName = data.EName;
            model.LogoImgPath = data.LogoImgPath;
            model.DataPath = data.DataPath;
            model.FilePath = data.FilePath;
            model.CopyrightInfo = data.CopyrightInfo;
            model.AssistCompany = data.AssistCompany;
            model.Phone = data.Phone;
            model.Fax = data.Fax;
            model.AQNo = data.AQNo;
            model.AQURL = data.AQURL;
            model.TecSupport = data.TecSupport;
            model.XZXKPhone = data.XZXKPhone;
            model.NetNo = data.NetNo;
            model.GWABNo = data.GWABNo;
            model.GWABURL = data.GWABURL;
            model.EmailCode = data.EmailCode;
            model.EmailSMTP = data.EmailSMTP;
            model.BottomImgPath = data.BottomImgPath;
            model.BottomImgURL = data.BottomImgURL;
            model.QRCodeImgPath = data.QRCodeImgPath;
            model.WaterMarkImg = data.WaterMarkImg;
            model.HaveSlideShow = data.HaveSlideShow;
            model.PostCode = data.PostCode;
            model.Email = data.Email;
            //model.SmsAccount = data.SmsAccount;
            //model.SmsPassword = data.SmsPassword;
            //model.SmsWebSite = data.SmsWebSite;

            await _base_ComapnyBus.UpdateDataAsync(model);
        }

        /// <summary>
        /// 删除子站信息
        /// </summary>
        /// <param name="ids">id数组</param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _base_ComapnyBus.DeleteDataAsync(ids);
        }

        /// <summary>
        /// 保存域名信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveDetail(Base_ComapnyData data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                await _ComapnyDataBusiness.AddDataAsync(data);
            }
            else
            {
                await _ComapnyDataBusiness.UpdateDataAsync(data);
            }
        }

        /// <summary>
        /// 删除域名信息
        /// </summary>
        /// <param name="ids">域名信息Id数组</param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeleteDetail(List<string> ids)
        {
            await _ComapnyDataBusiness.DeleteDataAsync(ids);
        }

        /// <summary>
        /// 修改域名状态
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SetDetailState(IdInputDTO input)
        {
            var model = _ComapnyDataBusiness.GetModel(input.id);
            model.State = !model.State;
            await _ComapnyDataBusiness.UpdateDataAsync(model);
        }

        /// <summary>
        /// 获取文章可报送站点
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public object GetRCompanys(IdInputDTO input)
        {

            return _ComapnyDataBusiness.GetRCompanys(input.id);
        }

        /// <summary>
        /// 设置文章可报送站点
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SetRCompanys(SetRCompanyDTO input)
        {
           
            await _ComapnyDataBusiness.SetRCompanys(input);
        }
        #endregion

        /// <summary>
        /// 获取文章报送站点列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public List<object> GetRCompanyList(IdInputDTO input)
        {

            return _ComapnyDataBusiness.GetRCompanyList(input.id);
        }
    }
}